﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLNS
{
    public partial class frmDoiMK : Form
    {
        public frmDoiMK()
        {
            InitializeComponent();
        }
        public SqlConnection getConnect()
        {
            return new SqlConnection(@"Data Source=THANGVUONG\SQLEXPRESS;Initial Catalog=Quanlynhansu;Integrated Security=True");
        }
        public void MK_Change(User u)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("MK_Update", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@user", u.user));
            cmd.Parameters.Add(new SqlParameter("@pass", u.pass));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }
        public bool KT_MK(string user,string pass)
        {
            bool temp = false;
            SqlConnection conn = getConnect();
            conn.Open();
            SqlCommand cmd = new SqlCommand("MK_Search", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@user", user));
            cmd.Parameters.Add(new SqlParameter("@pass", pass));
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if ((user == reader.GetString(0)) && (pass==reader.GetString(1)))
                {
                    temp = true;
                    break;
                }
            }
            conn.Close();
            return temp;
        }

        private void btDoimk_Click(object sender, EventArgs e)
        {
            User u = new User();
            u.user = txtManv.Text;
            u.pass = txtNewpass.Text;
            if (KT_MK(txtManv.Text, txtPass.Text) == true)
            {
                MK_Change(u);
                lbstt.Text = "";
                MessageBox.Show("Đổi mật khẩu thành công!");
            }
            else
            {
                lbstt.Text = "Tên đăng nhập hoặc mật khẩu cũ không đúng!";
                txtManv.Focus();
                return;
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
